import MoreGamesPanel from "./MoreGamesPanel";
import PlatUtils from "./PlatUtils";
import { utils } from "./Utils";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MoreGamesWidget extends cc.Component {

    @property(cc.Prefab)
    prefab : cc.Prefab = null;

    btnMoreGames : cc.Node = null;
    moreGamesPanel : MoreGamesPanel = null;

    onLoad(){

        this.btnMoreGames = this.node.getChildByName("Btn_MoreGames");
        this.btnMoreGames.active = false;

        let panel = cc.instantiate(this.prefab);
        this.moreGamesPanel = panel.getComponent("MoreGamesPanel");
        this.node.parent.addChild(panel);
    }

    onEnable(){
        cc.game.on("WeChatPlatMessage", this.onMessage, this);

        this.moreGamesPanel.hide();
        this.setBtnVisible();
    }

    onDisable(){
        cc.game.targetOff(this);
    }

    onMessage(event : any){
        switch(event.type){
            case "ConfigLoaded":{
                this.btnMoreGames.active = true;
                this.moreGamesPanel.updatePanel();
                break;
            }
        }
    }

    setBtnVisible(){
        if(utils.wechatTool && utils.wechatTool.configData){
            this.btnMoreGames.active = true;
        }else{
            this.btnMoreGames.active = false;
        }
    }

    onBtnClickedHandler(event: cc.Event, data:any){
        this.moreGamesPanel.show();
    }

    
}
